﻿using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Text;

namespace HPDI.DrawingStandard
{
    public class InitConfig
    {




        public static void  InitStandardConfig()
        {
            InitLayer();
            InitTextStyle("H仿宋", "仿宋", 0.7);
            InitDimStyle(Unit.Millimeter, Unit.Millimeter, 1, "H仿宋");
            InitDimStyle(Unit.Millimeter, Unit.Millimeter, 20, "H仿宋");
            InitBlock();
        }

        /// <summary>
        /// 初始化制图单位
        /// </summary>
        /// <param name="drawingUnit"></param>
        public static void InitUnits(UnitsValue drawingUnit = UnitsValue.Millimeters)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                db.Insunits = drawingUnit;
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord ms = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                ms.Units = drawingUnit;
                tr.Commit();
            }
        }


        /// <summary>
        /// 标准图层配置
        /// </summary>
        public static void InitLayer()
        {   
            Database acCurDb = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = acCurDb.TransactionManager.StartTransaction())
            {
                Dictionary<string, short> ldic = new Dictionary<string, short>()
                {
                    ["H粗线"] = 4,
                    ["H细线"] = 2,
                    ["H标注"] = 7,
                    ["H文本"] = 7,
                    ["H中心线"] = 1,
                    ["H虚线"] = 3,
                    ["H图框"] = 8,
                    ["H标注"] = 7,
                };
                List<string> Lname = new List<string>() { "CENTER", "DASHED" };
                LayerTable acLyrTbl;
                acLyrTbl = tr.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                LinetypeTable acLinTbl;
                acLinTbl = tr.GetObject(acCurDb.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
                foreach (string ltname in Lname)
                {
                    if (!acLinTbl.Has(ltname))
                    {
                        acCurDb.LoadLineTypeFile(ltname, "acad.lin");
                    }
                }
                LayerTableRecord acLyrTblRec = new LayerTableRecord();
                foreach (string key in ldic.Keys)
                {
                    short cid = ldic[key];
                    acLyrTblRec = new LayerTableRecord();
                    if (!acLyrTbl.Has(key))
                    {
                        acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, cid);
                        if (cid != 4) { acLyrTblRec.LineWeight = LineWeight.LineWeight009; }
                        else { acLyrTblRec.LineWeight = LineWeight.LineWeight030; }
                        if (cid == 1) { acLyrTblRec.LinetypeObjectId = acLinTbl["CENTER"]; }
                        if (cid == 3) { acLyrTblRec.LinetypeObjectId = acLinTbl["DASHED"]; }
                        if (cid == 8) { acLyrTblRec.IsPlottable = false; }
                        acLyrTblRec.Name = key;
                        if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen();
                        acLyrTbl.Add(acLyrTblRec);
                        tr.AddNewlyCreatedDBObject(acLyrTblRec, true);
                    }
                    else
                    {
                        acLyrTblRec = tr.GetObject(acLyrTbl[key], OpenMode.ForWrite) as LayerTableRecord;
                        if (cid != 4) { acLyrTblRec.LineWeight = LineWeight.LineWeight009; }
                        else { acLyrTblRec.LineWeight = LineWeight.LineWeight030; }
                        if (cid == 1) { acLyrTblRec.LinetypeObjectId = acLinTbl["CENTER"]; }
                        if (cid == 3) { acLyrTblRec.LinetypeObjectId = acLinTbl["DASHED"]; }
                        if (cid == 8) { acLyrTblRec.IsPlottable = false; }
                        acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, cid);
                    }
                }

                if (!acLyrTbl.Has("H设计线"))//新建的设置对象
                {
                    acLyrTblRec = new LayerTableRecord();
                    acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
                    acLyrTblRec.Name = "H设计线";
                    acLyrTblRec.LineWeight = LineWeight.LineWeight015;
                    if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen();
                    acLyrTbl.Add(acLyrTblRec);
                    tr.AddNewlyCreatedDBObject(acLyrTblRec, true);
                }
                if (!acLyrTbl.Has("H地面线"))
                {
                    acLyrTblRec = new LayerTableRecord();
                    acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 8);
                    acLyrTblRec.Name = "H地面线";
                    acLyrTblRec.LineWeight = LineWeight.LineWeight015;
                    if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen();
                    acLyrTbl.Add(acLyrTblRec);
                    tr.AddNewlyCreatedDBObject(acLyrTblRec, true);
                }
                tr.Commit();
            }
        }

        /// <summary>
        /// 初始化文字样式
        /// </summary>
        /// <param name="StyleName">文字样式名称</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="WidthFactor">宽度因子</param>
        /// <param name="charset">字符集，中文3212默认134</param>
        /// <param name="pf">字族，</param>
        public static void InitTextStyle(string StyleName, string FontName,double WidthFactor,int charset=134,int pf=48)
        {
            Database acCurDb = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = acCurDb.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(acCurDb.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
                #region 字体设置
                if (!st.Has(StyleName))//字体设置
                {
                    TextStyleTableRecord str = new TextStyleTableRecord()
                    {
                        Name = StyleName,
                        Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(FontName, false, false, charset, pf),
                        XScale = WidthFactor,
                    };
                    st.Add(str);
                    tr.AddNewlyCreatedDBObject(str, true);
                }
                else
                {
                    TextStyleTableRecord str = tr.GetObject(st[StyleName], OpenMode.ForWrite) as TextStyleTableRecord;
                    str.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(FontName, false, false, charset, pf);
                    str.XScale = WidthFactor;
                }
                #endregion
                tr.Commit();
            }
        }

        public static void InitLeaderStyle(double scale)
        {
            Database acCurDb = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = acCurDb.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(acCurDb.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
                #region 引线
                string StName = "ML";
                if (scale < 1)
                {
                    StName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    StName += "1-" + scale.ToString();
                }
                DBDictionary mlstyles = (DBDictionary)tr.GetObject(acCurDb.MLeaderStyleDictionaryId, OpenMode.ForRead);
                MLeaderStyle mldst;
                if (!mlstyles.Contains(StName))
                {
                    mldst = new MLeaderStyle();
                    mldst.TextHeight = 2.5;
                    mldst.TextStyleId = st["H仿宋"];
                    mldst.ArrowSize = 0;
                    mldst.TextAlignAlwaysLeft = true;
                    mldst.ExtendLeaderToText = false;
                    mldst.LandingGap = 0.2;
                    mldst.EnableDogleg = false;
                    mldst.TextAlignmentType = TextAlignmentType.LeftAlignment;
                    mldst.TextAttachmentDirection = TextAttachmentDirection.AttachmentHorizontal;
                    mldst.TextAngleType = TextAngleType.HorizontalAngle;
                    mldst.Scale = scale;
                    mldst.SetTextAttachmentType(TextAttachmentType.AttachmentBottomOfTopLine, LeaderDirectionType.LeftLeader);
                    mldst.SetTextAttachmentType(TextAttachmentType.AttachmentBottomOfTopLine, LeaderDirectionType.RightLeader);
                    ObjectId mLeaderStyle = mldst.PostMLeaderStyleToDb(acCurDb, StName);
                    tr.AddNewlyCreatedDBObject(mldst, true);
                }

                #endregion
                tr.Commit();
            }



        }


        /// <summary>
        /// 配置标注样式
        /// </summary>
        /// <param name="plotunit">绘图单位</param>
        /// <param name="paperunit">标注单位</param>
        /// <param name="scale">比例</param>
        /// <param name="tstyle">文字样式</param>
        public static void InitDimStyle(Unit plotunit, Unit paperunit, double scale, string tstyle)
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];


            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DimStyleTable dst = tr.GetObject(db.DimStyleTableId, OpenMode.ForWrite) as DimStyleTable;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;

                string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                DimStyleTableRecord dstr = new DimStyleTableRecord();
                if (!dst.Has(DimStyleName))
                {
                    dstr.Name = DimStyleName;
                    dstr.Dimscale = scale;
                    dstr.Dimtxsty = st[tstyle];
                    dstr.Dimclrd = Color.FromColorIndex(ColorMethod.ByAci, 6);
                    dstr.Dimclre = Color.FromColorIndex(ColorMethod.ByAci, 6);
                    dstr.Dimdli = 5;
                    dstr.Dimsah = true;
                    dstr.Dimdle = 0;
                    dstr.Dimexe = 1.0;
                    dstr.Dimexo = 1.0;
                    dstr.DimfxlenOn = true;
                    dstr.Dimfxlen = 4;
                    dstr.Dimtxt = 2.5;
                    dstr.Dimasz = 1.5;
                    dstr.Dimtix = true;
                    dstr.Dimtmove = 1;
                    dstr.Dimtad = 1;
                    dstr.Dimgap = 0.8;
                    dstr.Dimdec = 0;
                    dstr.Dimtih = false;
                    dstr.Dimtoh = false;
                    dstr.Dimdsep = '.';
                    dstr.Dimlfac = factor;
                    dst.Add(dstr);
                    tr.AddNewlyCreatedDBObject(dstr, true);
                }
                else
                {
                    dstr = tr.GetObject(dst[DimStyleName], OpenMode.ForWrite) as DimStyleTableRecord;
                    dstr.Dimscale = scale;
                    dstr.Dimtxsty = st[tstyle];
                    dstr.Dimclrd = Color.FromColorIndex(ColorMethod.ByAci, 6);
                    dstr.Dimclre = Color.FromColorIndex(ColorMethod.ByAci, 6);
                    dstr.Dimdli = 5;
                    dstr.Dimsah = true;
                    dstr.Dimdle = 0;
                    dstr.Dimexe = 1.0;
                    dstr.Dimexo = 1.0;
                    dstr.DimfxlenOn = true;
                    dstr.Dimfxlen = 4;
                    dstr.Dimtxt = 2.5;
                    dstr.Dimasz = 1.5;
                    dstr.Dimtix = true;
                    dstr.Dimtmove = 1;
                    dstr.Dimtad = 1;
                    dstr.Dimgap = 0.8;
                    dstr.Dimdec = 0;
                    dstr.Dimtih = false;
                    dstr.Dimtoh = false;
                    dstr.Dimdsep = '.';
                    dstr.Dimlfac = factor;
                }

                tr.Commit();
            }
        }

        /// <summary>
        /// 初始化文字比例图块
        /// </summary>
        public static void InitBlock()
        {
            Database acCurDb = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = acCurDb.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(acCurDb.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
                
                #region 自定义块
                BlockTable bt = (BlockTable)tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = new BlockTableRecord();
                if (!bt.Has("KYBG"))
                {
                    btr = new BlockTableRecord();
                    btr.Name = "KYBG";
                    bt.UpgradeOpen();
                    bt.Add(btr);
                    tr.AddNewlyCreatedDBObject(btr, true);
                    Polyline Paa = new Polyline();
                    Paa.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
                    Paa.AddVertexAt(1, new Point2d(200 / Math.Sqrt(3), 200), 0, 0, 0);
                    Paa.AddVertexAt(2, new Point2d(-200 / Math.Sqrt(3), 200), 0, 0, 0);
                    Paa.Closed = true;
                    btr.AppendEntity(Paa);
                    tr.AddNewlyCreatedDBObject(Paa, true);

                    Line Laa = new Line(new Point3d(-200 / Math.Sqrt(3), 0, 0), new Point3d(1200, 0, 0));
                    btr.AppendEntity(Laa);
                    tr.AddNewlyCreatedDBObject(Laa, true);

                    AttributeDefinition curbg = new AttributeDefinition();
                    curbg.Position = new Point3d(120, 80, 0);
                    curbg.Height = 300;
                    curbg.WidthFactor = st["H仿宋"].GetXScale();
                    curbg.Tag = "标高";
                    curbg.TextStyleId = st["H仿宋"];
                    btr.AppendEntity(curbg);
                    tr.AddNewlyCreatedDBObject(curbg, true);
                }
                if (!bt.Has("BG"))
                {
                    btr = new BlockTableRecord();
                    btr.Name = "BG";
                    bt.UpgradeOpen();
                    bt.Add(btr);
                    tr.AddNewlyCreatedDBObject(btr, true);
                    Polyline Paa = new Polyline();
                    Paa.AddVertexAt(0, new Point2d(0, 0), 0, 0, 200);
                    Paa.AddVertexAt(1, new Point2d(0, 200), 0, 0, 0);
                    btr.AppendEntity(Paa);
                    tr.AddNewlyCreatedDBObject(Paa, true);
                    AttributeDefinition curbg = new AttributeDefinition();
                    curbg.Position = new Point3d(120, 200, 0);
                    curbg.Height = 250;
                    curbg.WidthFactor = st["H仿宋"].GetXScale();
                    curbg.Tag = "标高";
                    curbg.TextStyleId = st["H仿宋"];
                    btr.AppendEntity(curbg);
                    tr.AddNewlyCreatedDBObject(curbg, true);
                }
                //-------------------------------------------------------------------------------------------
                if (!bt.Has("YP"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "YP";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                        new Point3d(774.9048 - 974.2305, 49.6974, 0),
                        new Point3d(710.2305 - 974.2305, 0, 0),
                        new Point3d(974.2305 - 974.2305, 0, 0)
                        )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);

                    Line L = new Line(new Point3d(974.2305 - 974.2305, 0, 0), 
                        new Point3d(-225.7695 - 974.2305, 0, 0))
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(-160 - 974.2305, 73.1925, 0);
                    curzp.Height = 300;
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "右坡";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }


                //-------------------------------------------------------------------------------------------
                if (!bt.Has("ZP"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "ZP";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                        new Point3d(-184.6346, 49.6974, 0),
                        new Point3d(-119.9603, 0, 0),
                        new Point3d(-383.9603, 0, 0)
                        )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);

                    Line L = new Line(new Point3d(-383.9603, 0, 0), new Point3d(816.0397, 0, 0))
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(-160, 73.1925, 0);
                    curzp.Height = 300;
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "左坡";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }
                //-------------------------------------------------------------------------------------------
                if (!bt.Has("TP"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "TP";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                        new Point3d(774.9048 - 374.2305, -49.6974, 0),
                        new Point3d(710.2305 - 374.2305, 0, 0),
                        new Point3d(974.2305 - 374.2305, 0, 0)
                        )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);
                    Polyline L = new Polyline() { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    L.AddVertexAt(0, new Point2d(974.2305 - 374.2305, 0), 0, 0, 0);
                    L.AddVertexAt(1, new Point2d(374.2305 - 374.2305, 0), 0, 0.5, 0.5);
                    L.AddVertexAt(2, new Point2d(374.2305 - 374.2305, -400), 0, 0.5, 0.5);

                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(474 - 374.2305, -373.1925, 0);
                    curzp.Height = 300;
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "A";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }


                //-------------------------------------------------------------------------------------------
                if (!bt.Has("BP"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "BP";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                       new Point3d(774.9048 - 374.2305, 49.6974, 0),
                       new Point3d(710.2305 - 374.2305, 0, 0),
                       new Point3d(974.2305 - 374.2305, 0, 0)
                       )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);
                    Polyline L = new Polyline() { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    L.AddVertexAt(0, new Point2d(974.2305 - 374.2305, 0), 0, 0, 0);
                    L.AddVertexAt(1, new Point2d(374.2305 - 374.2305, 0), 0, 0.5, 0.5);
                    L.AddVertexAt(2, new Point2d(374.2305 - 374.2305, 400), 0, 0.5, 0.5);

                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(474 - 374.2305, 73.1925, 0);
                    curzp.Height = 300;
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "A";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }

                if (!bt.Has("SP"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "SP";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                        new Point3d(49.6974, 774.9048 - 974.2305, 0),
                        new Point3d(0, 710.2305 - 974.2305, 0),
                        new Point3d(0, 974.2305 - 974.2305, 0)
                        )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);

                    Line L = new Line(new Point3d(0, 974.2305 - 974.2305, 0), new Point3d(0, -225.7695 - 974.2305, 0))
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(373.1925, -60 - 974.2305, 0);
                    curzp.Height = 300;
                    curzp.Rotation = GeTools.DegreeToRadian(90);
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "上坡";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }

                if (!bt.Has("YB"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "YB";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                       new Point3d(49.6974, -184.6346, 0),
                       new Point3d(0, -119.9603, 0),
                       new Point3d(0, -383.9603, 0)
                       )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);
                    Polyline L = new Polyline() { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    L.AddVertexAt(0, new Point2d(0, -383.9603), 0, 0, 0);
                    L.AddVertexAt(1, new Point2d(0, -383.9603 + 600), 0, 0, 0);
                    L.AddVertexAt(2, new Point2d(400, -383.9603 + 600), 0, 0, 0);
                    //Line L = new Line(new Point3d(974.2305 - 974.2305, 0, 0), new Point3d(-225.7695 - 974.2305, 0, 0))
                    //{ Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(110, -110, 0);
                    curzp.Height = 300;
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "B";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }


                //-------------------------------------------------------------------------------------------
                if (!bt.Has("ZB"))
                {
                    BlockTableRecord btr2 = new BlockTableRecord();
                    btr2.Name = "ZB";
                    bt.UpgradeOpen();
                    bt.Add(btr2);
                    tr.AddNewlyCreatedDBObject(btr2, true);

                    Solid Array = new Solid(
                        new Point3d(-49.6974, -184.6346, 0),
                        new Point3d(0, -119.9603, 0),
                        new Point3d(0, -383.9603, 0)
                        )
                    { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    btr2.AppendEntity(Array);
                    tr.AddNewlyCreatedDBObject(Array, true);
                    Polyline L = new Polyline() { Color = Color.FromColorIndex(ColorMethod.ByAci, 7), };
                    L.AddVertexAt(0, new Point2d(0, -383.9603), 0, 0, 0);
                    L.AddVertexAt(1, new Point2d(0, -383.9603 + 600), 0, 0, 0);
                    L.AddVertexAt(2, new Point2d(-400, -383.9603 + 600), 0, 0, 0);
                    btr2.AppendEntity(L);
                    tr.AddNewlyCreatedDBObject(L, true);

                    AttributeDefinition curzp = new AttributeDefinition();
                    curzp.Position = new Point3d(-373.1925, -110, 0);
                    curzp.Height = 300;
                    curzp.WidthFactor = st["H仿宋"].GetXScale();
                    curzp.Tag = "B";
                    curzp.TextStyleId = st["H仿宋"];
                    btr2.AppendEntity(curzp);
                    tr.AddNewlyCreatedDBObject(curzp, true);
                }
                #endregion
                tr.Commit();
            }
        }

    }
}
